package top.jnaw.jee.platform.controller.basics.extend;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.Badge;
import top.jnaw.jee.platform.model.BadgeUser;
import top.jnaw.jee.platform.model.Kcgl;
import top.jnaw.jee.platform.model.KcglSteps;
import top.jnaw.jee.platform.model.KcglUser;
import top.jnaw.jee.platform.model.Steps;
import top.jnaw.jee.utils.Log;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Strings;

public class BadgeController extends Controller {


  public void list() {
    JSONObject req = Strings.toJSONObject(this);
    final int page = Strings.getInt(req, "page", Consts.DEFAULT_PAGE);
    final int size = Strings.getInt(req, "size", Consts.DEFAULT_PAGE_SIZE);
    final String keyWord = Strings.getString(req, "keyWord", "");
    renderJson(Strings
        .formatJsonDateTime(
            Strings.formatPage(Badge.list(page, size, keyWord))));
  }

  public void listAll() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final String openid = Strings.getString(req, "openid", "");
    final String type = Strings.getString(req, "type", "");
    if (StrKit.notBlank(openid)) {
      result = Badge.listAll(openid, type);
    }
    renderJson(result.toJson());
  }

  public void update() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    final int fid = Strings.getInt(req, "fid", Consts.DEFAULT_BAD_ID_1);
    final String title = Strings.getString(req, "title", "");
    final String content = Strings.getString(req, "content", "");
    final String type = Strings.getString(req, "type", "other");
    if (id > 0 && fid > 0 && StrKit.notBlank(title)) {
      int bid = Badge.update(id, fid, title, content, type);
      if (bid > 0) {
        result.setResult(Result.kSuccess);
      }
    }
    renderJson(result.toJson());
  }

  public void add() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int fid = Strings.getInt(req, "fid", Consts.DEFAULT_BAD_ID_1);
    final String title = Strings.getString(req, "title", "");
    final String content = Strings.getString(req, "content", "");
    final String type = Strings.getString(req, "type", "other");

    if (fid > 0 && StrKit.notBlank(title)) {
      int bid = Badge.add(fid, title, content, type);
      if (bid > 0) {
        result.setResult(Result.kSuccess);
      }
    }
    renderJson(result.toJson());
  }

  public void del() {
    ModelResult result = new ModelResult(Result.kModelDeleteError);
    JSONObject req = Strings.toJSONObject(this);
    JSONArray ids = Strings.getJSONArray(req, "ids", null);
    if (null != ids && ids.size() > 0) {
      int r = 0;
      boolean res = false;
      boolean haveKc = false;
      for (int i = 0; i < ids.size(); i++) {
        String type = Badge.getType(ids.getInteger(i));
        if (type.equals("kcgl")) {
          haveKc = true;
        } else {
          if (Badge.delete(ids.getInteger(i))) {
            res = true;
          }
        }
      }
      if (haveKc) {// 有课程徽章,不能删除 result=26
        result.setResult(Result.kBadId);
      } else {
        if (res) {
          result.setResult(Result.kSuccess);
        }
      }
    }
    renderJson(result.toJson());
  }

  public void query() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    if (id > 0) {
      result = Badge.query(id);
    }
    renderJson(result.toJson());
  }

  public void updateUser() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final int bid = Strings.getInt(req, "bid", Consts.DEFAULT_BAD_ID);
    final String openid = Strings.getString(req, "openid", "");
    final String type = Strings.getString(req, "type", "");
    if (bid > 0 && StrKit.notBlank(openid)) {
      result = BadgeUser.updateUser(bid, openid, type);
    }
    renderJson(result.toJson());
  }

}
